package jupiter.message;
import java.io.Serializable;

import jupiter.node.Address;

import org.json.JSONException;
import org.json.JSONObject;

/*
{
  type: "node"
  action: "create"
  from: "jmvc.com@jabbify.com"
  to: "justin_presence#jmvc.com@jabbify.com"
  data : {
    only:  ['hank','john'] //only allow these nodes to add listener
    block: ['fred'] //do not allow these nodes to add listener
    nodes: ['jmvc'] //which nodes I subscribe to
  }
}
 */

public class Message implements Serializable{

	private String message;
	private JSONObject json;
	private Address _to;
	private Address _from;
	private String _type;
	private String _action;
	public JSONObject data;
	
	void NodeMessage(String mess){
		message = mess;
	}
	void parseAndPrepare(){
		try{
			json = new JSONObject(message);
			if(json.has("to")) 		_to =  new Address(json.getString("to"));
			if(json.has("from")) 	_from =  new Address(json.getString("from"));
			_type =  json.getString("type");
			_action =  json.getString("action");
			data = json.getJSONObject("data");
		}catch(JSONException e){	
			
		}
	}
	public Address to(){
		return _to;
	}
}
